package com.sys.SysAI.controller;

import com.sys.SysAI.service.SysAiWaterQualityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/water-quality")
@CrossOrigin // 允许跨域
public class SysAiWaterQualityController {

    private static final Logger log = LoggerFactory.getLogger(SysAiWaterQualityController.class);

    @Autowired
    private SysAiWaterQualityService sysWaterQualityService;

    /**
     * 接收水质参数并返回包含具体调整建议的分析结果
     */
    @PostMapping("/analyze")
    public Map<String, Object> analyze(@RequestBody Map<String, Object> parameters) {
        // 参数校验
        if (parameters == null || parameters.isEmpty()) {
            log.warn("水质参数为空");
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("status", "error");
            errorResult.put("message", "水质参数不能为空");
            return errorResult;
        }
        try {
            // 调用服务层进行分析
            Map<String, String> advice = sysWaterQualityService.analyzeWaterQuality(parameters);

            // 返回结构化结果
            Map<String, Object> result = new HashMap<>();
            result.put("status", "success");
            result.put("data", advice);
            return result;
        } catch (RuntimeException e) {
            log.error("分析水质时发生错误", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("status", "error");
            errorResult.put("message", e.getMessage());
            return errorResult;
        }
    }
}